package com.itic.appbase.applications.sys.token.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.sys.token.persistence.dao.TokenDao;
import com.itic.appbase.applications.sys.token.persistence.model.Token;
import com.itic.appbase.applications.sys.token.persistence.model.TokenExample;
import com.itic.appbase.applications.sys.token.persistence.model.TokenExample.Criteria;
import com.itic.appbase.applications.sys.user.persistence.model.SysUser;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.CalendarHelper;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.RandomOrderNumHelper;


@Service
@Transactional
public class TokenService
     extends CrudService<TokenDao,Token,TokenExample> {
    @Autowired
    private TokenDao tokenDao;

    /**
     * 刷新token的过期时间
     * @param token
     * @param device
     * @return
     */
	public Token flushToken(String token, String device) {
		Token result = null;
		TokenExample example = new TokenExample();
		Criteria criteria = example.createCriteria();
		criteria.andAccessTokenEqualTo(token);
		criteria.andDeviceEqualTo(device);
		
		List<Token> list = tokenDao.selectByExample(example);
		if( !list.isEmpty() ){
			result = list.get(0);
			result.setExpiredTime(getExpiredTimeToken(new Date()));
			tokenDao.updateByPrimaryKeySelective(result);
		}
		return result;
	}
	
	/**
     * 获取 Token的过期 时间
     * @param date
     * @return
     */
    public Date getExpiredTimeToken(Date date){
    	Date dateAfter = CalendarHelper.getDateAfter(date, 7);
    	return dateAfter;
    }


    /**
     * 获得用户token
     * @param phone
     * @param device
     * @return
     */
	public Token getTokenByPhoneAndDevice(SysUser user, String device) {
		Token token = null;
		TokenExample example = new TokenExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserPhoneEqualTo(user.getMobile());
		criteria.andDeviceEqualTo(device);
		criteria.andIsOffsiteEqualTo(DBConst.FALSE);
		criteria.andExpiredTimeGreaterThanOrEqualTo(new Date());
		List<Token> list = tokenDao.selectByExample(example);
		if( list.isEmpty() ){
			//创建token
			token = new Token();
			this.initDefalutFields(token);
			token.setUserPhone(user.getMobile());
			token.setDevice(device);
			token.setIsOffsite(DBConst.FALSE);
			token.setExpiredTime(getExpiredTimeToken(new Date()));
			token.setUserNickName(user.getRealname());
			token.setAccessToken(getToken());
			token.setUserId(user.getId());
			token.setIsOffsite(DBConst.FALSE);
			tokenDao.insert(token);
		}else{
			token = list.get(0);
			//token刷新
			this.flushToken(token.getAccessToken(), token.getDevice());
		}
		return token;
	}
	
	/**
     * 获取随机TOKEN方法
     */
    public String getToken(){
    	String orderNumber = RandomOrderNumHelper.getOrderNumber();
    	String uuid = IdGen.uuid();
    	return orderNumber + uuid;
    }


}